package com.folioreader.util;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.folioreader.Config;
import com.folioreader.Constants;
import com.github.kittinunf.fuel.android.core.Json;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AppUtilNew {

    private enum FileType {
        OPS,
        OEBPS,
        NONE
    }

    public static String charsetNameForURLConnection(URLConnection connection) {
        String contentType = connection.getContentType();
        String[] values = contentType.split(";");
        String charset = null;
        for (String value : values) {
            value = value.trim();

            if (value.toLowerCase().startsWith("charset=")) {
                charset = value.substring("charset=".length());
            }
        }

        if (charset == null || charset.isEmpty()) {
            charset = "UTF-8"; //Assumption
        }
        return charset;
    }

    public static String formatDate(Date hightlightDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT, Locale.getDefault());
        return simpleDateFormat.format(hightlightDate);
    }

    public static void saveConfig(Context context, Config config) {
        try {
            JSONObject obj = new JSONObject();
            obj.put(Config.CONFIG_FONT, config.getFont());
            obj.put(Config.CONFIG_FONT_SIZE, config.getFontSize());
            obj.put(Config.CONFIG_IS_NIGHT_MODE, config.isNightMode());
            obj.put(Config.CONFIG_THEME_COLOR_INT, config.getThemeColor());
            obj.put(Config.CONFIG_IS_TTS, config.isShowTts());
            obj.put(Config.CONFIG_ALLOWED_DIRECTION, config.getAllowedDirection().toString());
            obj.put(Config.CONFIG_DIRECTION, config.getDirection().toString());

            SharedPreferenceUtil.putSharedPreferencesString(context, Config.INTENT_CONFIG, obj.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Config getSavedConfig(Context context) {
        String json = SharedPreferenceUtil.getSharedPreferencesString(context, Config.INTENT_CONFIG, null);
        if (json != null) {
            try {
                JSONObject jsonObject = new JSONObject(json);
                return new Config(jsonObject);
            } catch (JSONException e) {
                return null;
            }

        }
        return null;
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        View view = activity.getCurrentFocus();
        if (view != null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        view.clearFocus();
    }

    public static int getAvailablePortNumber(int portNumber) {
        ServerSocket serverSocket = null;
        int portNumberAvailable;

        try {
            serverSocket = new ServerSocket(portNumber);
            portNumberAvailable = portNumber;

        } catch (IOException e) {
            e.printStackTrace();
            try {
                serverSocket = new ServerSocket(0);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            portNumberAvailable = serverSocket.getLocalPort();
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return portNumberAvailable;
    }

}

